#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

int vallue = 0;
pthread_mutex_t mutex;

void*func(void* arg)
{
    unsigned int* p = (unsigned int*)arg;
    while(1)
    {
        pthread_mutex_lock(&mutex); // 加锁
        if ((*p) % 2 == 0)
        {
            printf("偶数:%d\n", *p);
        }
        pthread_mutex_unlock(&mutex); // 解锁
        
        // sleep(1);
    }
}

int main(int argc, char const *argv[])
{
    pthread_mutex_init(&mutex, NULL);

    // 创建一个线程
    pthread_t tid;
    pthread_create(&tid, NULL, func, &vallue);

    while(1)
    {
        pthread_mutex_lock(&mutex); // 加锁
        vallue++;  
        pthread_mutex_unlock(&mutex); // 解锁 
    }


    pthread_join(tid, NULL);

    return 0;
}
